#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class SubsidyForStudentProvider : CY.GFive.Core.Providers.DALProvider.ISubsidyForStudentProvider
	{
		#region Sql
		private static readonly string SqlSelectSubsidyForStudent = "SELECT [Id], [SubsidyName],[SubsidyDescription],[BeginDate] FROM [SubsidyForStudent] WHERE  [Id]=@Id";

		private static readonly string SqlInsertSubsidyForStudent = "INSERT INTO [SubsidyForStudent]([SubsidyName],[SubsidyDescription],[BeginDate]) values(@SubsidyName,@SubsidyDescription,@BeginDate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateSubsidyForStudent = "UPDATE [SubsidyForStudent] SET [SubsidyName] = @SubsidyName,[SubsidyDescription] = @SubsidyDescription,[BeginDate] = @BeginDate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteSubsidyForStudent = "DELETE [SubsidyForStudent] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllSubsidyForStudent = "SELECT [Id], [SubsidyName],[SubsidyDescription],[BeginDate] FROM [SubsidyForStudent]";
		#endregion

		#region ISubsidyForStudentProvider Members
		public Core.Business.SubsidyForStudent Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectSubsidyForStudent);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.SubsidyForStudent subsidyForStudent = new Core.Business.SubsidyForStudent();

				if (!reader.IsDBNull(0)) subsidyForStudent.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) subsidyForStudent.SubsidyName = reader.GetString(1);
				if (!reader.IsDBNull(2)) subsidyForStudent.SubsidyDescription = reader.GetString(2);
				if (!reader.IsDBNull(3)) subsidyForStudent.BeginDate = reader.GetDateTime(3);

				return subsidyForStudent;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.SubsidyForStudent subsidyForStudent)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SubsidyName", SqlDbType.NVarChar, subsidyForStudent.SubsidyName);
			sql.AddParameter("@SubsidyDescription", SqlDbType.NVarChar, subsidyForStudent.SubsidyDescription);
			sql.AddParameter("@BeginDate", SqlDbType.DateTime, subsidyForStudent.BeginDate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertSubsidyForStudent);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) subsidyForStudent.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.SubsidyForStudent subsidyForStudent)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@SubsidyName", SqlDbType.NVarChar, subsidyForStudent.SubsidyName);
			sql.AddParameter("@SubsidyDescription", SqlDbType.NVarChar, subsidyForStudent.SubsidyDescription);
			sql.AddParameter("@BeginDate", SqlDbType.DateTime, subsidyForStudent.BeginDate);
			sql.AddParameter("@Id", SqlDbType.Int, subsidyForStudent.Id);
			sql.ExecuteSql(SqlUpdateSubsidyForStudent);
		}

		public void Delete(Core.Business.SubsidyForStudent subsidyForStudent)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, subsidyForStudent.Id);
			sql.ExecuteSql(SqlDeleteSubsidyForStudent);
		}

		public IList<Core.Business.SubsidyForStudent> GetAllSubsidyForStudent()
		{
			IList<Core.Business.SubsidyForStudent> subsidyForStudentlist = new List<Core.Business.SubsidyForStudent>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllSubsidyForStudent);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.SubsidyForStudent subsidyForStudent = new Core.Business.SubsidyForStudent();

					if (!reader.IsDBNull(0)) subsidyForStudent.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) subsidyForStudent.SubsidyName = reader.GetString(1);
					if (!reader.IsDBNull(2)) subsidyForStudent.SubsidyDescription = reader.GetString(2);
					if (!reader.IsDBNull(3)) subsidyForStudent.BeginDate = reader.GetDateTime(3);

					subsidyForStudent.MarkOld();
					subsidyForStudentlist.Add(subsidyForStudent);
				}
				reader.Close();
			}
			return subsidyForStudentlist;
		}
		#endregion
	}
}
